import ReactDOM from 'react-dom/client';
import App from './App';
import { HashRouter } from 'react-router-dom'

//获取session
import { getSession } from '../src/utils/axiosRequest/api'
//缓存
import { putCache, removeCache } from '../src/utils/cache'
import { session_cache_key } from '../src/utils/cache/cacheKey'
//设备id
import { generateSessionParams } from "../src/utils/equipmentId/index"

//session加载失败
import SessionFail from './component/sessionFall'
//加载组件
import PageLoading from './component/pageLoading'

// 初始化函数
const initializeApp = async () => {
  //现展示加载状态
  const loadRoot = createRootView();
  loadRoot.render(<PageLoading ></PageLoading>);

  const normalRoot = createRootView();
  normalRoot.render(
    <HashRouter>
      <App />
    </HashRouter>
  );

  // try {
  //   const response = await getSession(generateSessionParams());
  //   //先删除所有缓存
  //   removeCache()
  //   //保存session
  //   const data = response.data
  //   putCache(session_cache_key, data.data.sessionId);
  //   loadRoot.unmount()//先卸载加载状态
  //   const normalRoot = createRootView();
  //   normalRoot.render(
  //     <HashRouter>
  //       <App />
  //     </HashRouter>
  //   );
  // } catch (error) {
  //   loadRoot.unmount()//先卸载加载状态
  //   const failRoot = createRootView();
  //   failRoot.render(<SessionFail ></SessionFail>);
  // }

};

const createRootView = () => {
  return ReactDOM.createRoot(document.getElementById('root'))
}

initializeApp();